let sql = require('../sql/sql');
let data = require('../libs/data');
var redis = require('redis').createClient();
let activity = require('../service/activity');
var db = require('../libs/db');

module.exports = {
    start(req,res){
        console.log('------开始答题------');
        var code = req.body.activityid;
        var time = (new Date()).valueOf();
    },
    // 结束答题
    end (req, res) {
        console.log('------结束答题------');
        var code = req.body.activityid;

        if (activity.updateActive(code)) {

        } else {
            data.code = '104';
            data.msg = '结束活动失败';
            res.status(200).send(JSON.stringify(data));
        }

    },
    // 获取活动缓存
    getActivityTime (req, res) {
        console.log('------获取活动缓存------');
        var code = req.body.code;
        data = {};
        redis.get('activityid_start_time_' + code, function (err, object) {
            if (!err && object != null) {
                data.code = '200';
                data.msg = '查询成功';
                data.time = object;
                data.nowtime = Date.parse(new Date());
                res.status(200).send(data);
            }else{
                data.code = '101';
                data.msg = '查询失败';
                res.status(200).send(data);
            }
        });
    },
    //添加用户
    addUser(req,res){
        var sql = 'select * from jyj_user a where a.flag = 2 limit 150,200';
        db.sequelize.query(sql, {
            type: db.sequelize.QueryTypes.SELECT
        }).then(function (result) {
            result.forEach(function (item,i) {

            })
            data.code = '200';
            data.msg = '查询数据成功';
            data.data = result;
            res.status(200).send(JSON.stringify(data));
        })
    },
    //查询群用户
    queryTribeUser(req,res){

    },
    //群加人
    addTribeUser(req,res){
        var sql = 'select * from jyj_user a where a.flag = 2';
        db.sequelize.query(sql, {
            type: db.sequelize.QueryTypes.SELECT
        }).then(function (result) {
            result.forEach(function (item,i) {

            })
            data.code = '200';
            data.msg = '查询数据成功';
            data.data = result;
            res.status(200).send(JSON.stringify(data));
        })
    }
};